// miniprogram/pages/home/home.js
Page({
  // 页面的初始数据
  data: {
    page: 0,
    ismore: true,
    complete: true,
    showShopingContent: false,
    showMarsk: false,
    goods: [],
    shopingCart: {},
    shopingCartArray: [],
    shopingCartLen: 0,
    shopingCartNowPrice: 0.00,
    shopingCartOldPrice: 0.00,
  },
  // 加载数据
  getGoods() {
    if (!this.data.complete) return false
    if (!this.data.ismore) return wx.stopPullDownRefresh()
    this.setData({complete: false})
    wx.cloud.callFunction({
      name: 'goods',
      data: {
        page: this.data.page
      }
    })
    .then(({result}) => {
      const {data} = result
      // 数据不够后面没有数据
      if (data && data.length < 10) {
        this.setData({
          ismore: false,
          complete: true,
          goods: this.data.goods.concat(data)
        })
      }else {
        // 正常有数据
        this.setData({
          ismore: true,
          goods: this.data.goods.concat(data),
          complete: true,
          page: this.data.page + 1
        })
      }
      wx.stopPullDownRefresh()
    })
    .catch(() => {
      this.setData({complete: true})
      wx.stopPullDownRefresh()
    })
  },
  // 修改数量
  action(e) {
    let item = e.currentTarget.dataset.item
    let _type = e.currentTarget.dataset.type | 0
    let _shopingCart = this.data.shopingCart
    let _shopingCartLen = this.data.shopingCartLen
    let _shopingCartNowPrice = this.data.shopingCartNowPrice
    let _shopingCartOldPrice = this.data.shopingCartOldPrice
    if (!_shopingCart[item._id]) _shopingCart[item._id] = item
    // 添加
    if (_type) {
      _shopingCart[item._id].number ++
      _shopingCartLen ++
      // 修改价格计数器
      _shopingCartNowPrice += parseFloat(item.nowPrice)
      _shopingCartOldPrice += parseFloat(item.oldPrice)
    }else {
      _shopingCart[item._id].number --
      _shopingCartLen --
      // 修改价格计数器
      _shopingCartNowPrice -= parseFloat(item.nowPrice)
      _shopingCartOldPrice -= parseFloat(item.oldPrice)
      // 改商品为0
      if (_shopingCart[item._id].number <= 0) delete _shopingCart[item._id]
      // 购物车为空关闭列表
      if (_shopingCartLen <= 0) this.toggleShopingContent(false)
    }
    this.setData({
      shopingCart: _shopingCart,
      shopingCartArray: Object.values(_shopingCart),
      shopingCartLen: _shopingCartLen,
      shopingCartNowPrice: _shopingCartNowPrice,
      shopingCartOldPrice: _shopingCartOldPrice,
    })
  },
  // 显示隐藏购物车列表
  toggleShopingContent(status = true) {
    if (status && !this.data.showShopingContent) {
      // 购物车为空，禁止许打开购物车
      if (!this.data.shopingCartLen) return false
      this.setData({
        showShopingContent: true
      })
      setTimeout(() => {
        this.setData({
          showMarsk: true
        })
      }, 10)
    }else {
      this.setData({
        showMarsk: false
      })
      setTimeout(() => {
        this.setData({
          showShopingContent: false
        })
      }, 300)
    }
  },
  // 确认点餐
  confirm() {
    const _this = this
    if (!this.data.shopingCartLen) return false
    wx.showModal({
      // title: '点餐',
      content: '确认后无法修改，是否继续？',
      confirmColor: "#1cb991",
      success (res) {
        if (res.confirm) {
          wx.showLoading({
            title: '加载中...',
          })
          wx.cloud.callFunction({
            name: 'addsold',
            data: {
              sold: _this.data.shopingCartArray
            }
          })
          .then(({result}) => {
            _this.setData({
              shopingCart: {},
              shopingCartArray: [],
              shopingCartLen: 0,
              shopingCartNowPrice: 0.00,
              shopingCartOldPrice: 0.00,
            })
            wx.hideLoading()
            wx.showModal({
              content: '点餐成功，请耐心等待送餐',
              confirmColor: "#1cb991",
              showCancel: false,
            })
          })
          .catch(() => wx.hideLoading())
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  // 生命周期函数--监听页面加载
  onLoad: function (options) {
    // wx.hideTabBar()
    wx.startPullDownRefresh()
  },
  onUnload() {
    wx.showTabBar()
  },
  // 页面相关事件处理函数--监听用户下拉动作
  onPullDownRefresh: function () {
    this.setData({
      goods: [],
      page: 0,
      ismore: true
    })
    this.getGoods()
  },
  // 页面上拉触底事件的处理函数
  onReachBottom: function () {
    this.getGoods()
  },
  // 用户点击右上角分享
  onShareAppMessage: function () {

  }
})